<template>
  <div id="app">
    <router-view />
  </div>
</template>
<script>
import menu from "./utils/meunList.js";
export default {
  name: "app",
  mounted() {
    // 权限验证 动态路由
    if (window.location.href.indexOf("login") < 0) {
      let permissions = localStorage.getItem("mb_permissions") || "[]";
      permissions = JSON.parse(permissions);
      let menuList = [];

      menu.forEach(item => {
        let itemList = null;
        if (permissions) {
          // 兼容ie9
          var findParentArr = permissions.filter(x => {
            return x.id == item.cover;
          });
          var findParent = findParentArr[0];
        }
        if (findParent) {
          itemList = JSON.parse(JSON.stringify(item));
          itemList.list = [];
          menuList.push(itemList);
        }

        item.list.forEach(v => {
          if (permissions) {
            // 兼容ie9
            var findArr = permissions.filter(x => {
              return x.id == v.cover;
            });
            var find = findArr[0];
          }
          if (find) {
            this.$router.options.routes[1].children.push(v.router);
            menuList.forEach(z => {
              if (z.cover == find.parentId) {
                z.list.push(v.router);
              }
            });
          }
        });
      });
      this.$router.addRoutes([this.$router.options.routes[1]]); // 调用add;
      this.$store.dispatch("setMenu", menuList);
    }
  }
};
</script>

<style lang="scss">
#app {
  height: 100%;
}
</style>
